using System.Collections.Generic;
using System.Linq;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace DA.AnimGraph.Editor {
    public class AGEPreview {

        public AGEPreviewPlayer[] players;

        public AGEPreview() {
            EditorSceneManager.sceneOpened += OnSceneOpened;
        }
        private void OnSceneOpened(Scene scene, OpenSceneMode mode) {
            GetPlayersInScene();
        }

        internal void GetPlayersInScene() {
            // 查找所有带有 AGEPreviewPlayer 组件的物体
            players = Object.FindObjectsOfType<AGEPreviewPlayer>();
        }

        public List<AGEPreviewPlayer> GetPreviewPlayerList() {
            if (players == null) {
                return null;
            } else {
                return players.ToList();
            }
        }

        public AGEPreviewPlayer GetPlayer(string name) {
            if (players == null) {
                return null;
            } else {
                var p = players.FirstOrDefault(p => p.gameObject.name == name);
                if (p == null || p.isHolding) {
                    return null;
                }
                return p;
            }
        }
    }

}
